/*
	Programação Concorrente
	Trabalho 2
	Turma A

	Adilson Abreu Couto Rapozo Junior ( n. USP: 6402336 )
	Luiz Alfredo Bertoni ( n. USP: 6418834 )
	Rodrigo Fernandes da Costa ( n. USP: 6793073 )
*/

#include "main.h"

void getWords ( char * pFileBuffer , int fileBufferSize , char ** ppWordBuffer , int * pWordNumber , int * pWordSize ) {

	int i , j , k ;
	int inWord = 0 ; /* Indicates if the pogram is reading a word or skiping characters. */

	int wordSize ; /* Number of characters in a word. */
	int maxWordSize ; /* Max number of characters in a word. */
	int wordNumber ; /* Number of words in the file. */

	char * pSingleWord ; /* Buffer for a single word. */

	/* Calculete word size and word number */

	for ( maxWordSize = wordNumber = wordSize = i = 0 ; i < fileBufferSize ; ++ i ) {

		if ( isalpha ( pFileBuffer [ i ] ) ) { /* Only letters are consider to be part of a word. */

			++ wordSize ;
			inWord = 1 ;

		} else {

			if ( inWord ) {

				if ( maxWordSize < wordSize ) {

					maxWordSize = wordSize ;
				}

				++ wordNumber ;
				wordSize = 0 ;
			}

			inWord = 0 ;
		}
	}

	* pWordNumber = wordNumber ;
	* pWordSize = wordSize = maxWordSize + 1 ; /* Take into account the '\0' */

	/* Retrieve words. */

	* ppWordBuffer = malloc ( wordSize * wordNumber ) ;
	pSingleWord = calloc ( 1 , wordSize ) ;

	for ( k = j = i = 0 ; i < fileBufferSize ; ++ i ) {

		if ( isalpha ( pFileBuffer [ i ] ) ) {

			pSingleWord [ j ++ ] = pFileBuffer [ i ] ;
			inWord = 1 ;

		} else {

			if ( inWord ) {

				memcpy ( * ppWordBuffer + k * wordSize , pSingleWord , wordSize ) ;
				memset ( pSingleWord , '\0' , wordSize ) ; /* Clean the buffer to receive the next word. */

				++ k ;
				j = 0 ;
			}

			inWord = 0 ;
		}
	}

	free ( pSingleWord ) ;
}

int isPhraseSeparator ( char character ) {

	int is ; /* Indicates if the character is a phrase separator. */

	is = character == '.' ; /* "." was considered a pharase separator. */
	is = is || ( character == '!' ) ; /* "!" was considered a pharase separator. */
	is = is || ( character == '?' ) ; /* "?" was considered a pharase separator. */

	return is ;
}

void getPhrases ( char * pFileBuffer , int fileBufferSize , char ** ppPhraseBuffer , int * pPhraseNumber , int * pPhraseSize ) {

	int i , j , k ;
	int inPhrase = 0 ; /* Indicates if the pogram is reading a phrase or skiping characters. */

	int phraseSize ; /* Number of characters in a phrase. */
	int maxPhraseSize ; /* Max number of characters in a phrase. */
	int phraseNumber ; /* Number of phrases in the file. */

	char * pSinglePhrase ; /* Buffer for a single phrase. */

	/* Calculete phrase size and phrase number */

	for ( maxPhraseSize = phraseNumber = phraseSize = i = 0 ; i < fileBufferSize ; ++ i ) {

		if ( ! isPhraseSeparator ( pFileBuffer [ i ] ) ) {

			if ( isalpha ( pFileBuffer [ i ] ) ) { /* Only letters are taken into account. */

				++ phraseSize ;

				inPhrase = 1 ;
			}

		} else {

			if ( inPhrase ) {

				if ( maxPhraseSize < phraseSize ) {

					maxPhraseSize = phraseSize ;
				}

				++ phraseNumber ;
				phraseSize = 0 ;
			}

			inPhrase = 0 ;
		}
	}

	* pPhraseNumber = phraseNumber ;
	* pPhraseSize = phraseSize = maxPhraseSize + 1 ; /* Take into account the '\0' */

	/* Retrieve phrases. */

	* ppPhraseBuffer = malloc ( phraseSize * phraseNumber ) ;
	pSinglePhrase = calloc ( 1 , phraseSize ) ;

	for ( k = j = i = 0 ; i < fileBufferSize ; ++ i ) {

		if ( ! isPhraseSeparator ( pFileBuffer [ i ] ) ) {

			if ( isalpha ( pFileBuffer [ i ] ) ) {

				pSinglePhrase [ j ++ ] =  pFileBuffer [ i ] ;

				inPhrase = 1 ;
			}

		} else {

			if ( inPhrase ) {

				memcpy ( * ppPhraseBuffer + k * phraseSize , pSinglePhrase , phraseSize ) ;
				memset ( pSinglePhrase , '\0' , phraseSize ) ; /* Clean the buffer to receive the next phrase. */

				++ k ;
				j = 0 ;
			}

			inPhrase = 0 ;
		}
	}
}

void readFile ( char * pFileName , char ** ppFileBuffer , int * pFileBufferSize ) {

	FILE * pFile ;

	* pFileBufferSize = 0 ;

	pFile = fopen ( pFileName , "r" ) ;
	
	/* Calculate the size of the file */

	while ( getc ( pFile ) != EOF ) {

		* pFileBufferSize += 1 ;
	}

	* ppFileBuffer = malloc ( * pFileBufferSize ) ;

	rewind ( pFile ) ;

	/* Read the file */

	fread ( * ppFileBuffer , 1 , * pFileBufferSize , pFile ) ;

	fclose ( pFile ) ;
}

